package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.LRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LEmployeeMapper;
import com.ruoyi.system.domain.LEmployee;
import com.ruoyi.system.service.ILEmployeeService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 员工Service业务层处理
 *
 * @author zyb
 * @date 2022-01-02
 */
@Service
public class LEmployeeServiceImpl extends ServiceImpl<LEmployeeMapper, LEmployee> implements ILEmployeeService {
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询员工
     *
     * @param empid 员工主键
     * @return 员工
     */
    @Override
    public LEmployee selectLEmployeeByEmpid(Integer empid) {
        return baseMapper.selectById(empid);
    }

    /**
     * 查询员工列表
     *
     * @param lEmployee 员工
     * @return 员工
     */
    @Override
    public List<LEmployee> selectLEmployeeList(LEmployee lEmployee) {
        return baseMapper.selectLEmployeeList(lEmployee);
    }

    /**
     * 新增员工
     *
     * @param lEmployee 员工
     * @return 结果
     */
    @Override
    public int insertLEmployee(LEmployee lEmployee) {
        lEmployee.setCreateBy(String.valueOf(getUserId()));
        lEmployee.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insert(lEmployee);
        getRedisList(0);
        return i;
    }

    /**
     * 修改员工
     *
     * @param lEmployee 员工
     * @return 结果
     */
    @Override
    public int updateLEmployee(LEmployee lEmployee) {
        lEmployee.setUpdateBy(String.valueOf(getUserId()));
        lEmployee.setUpdateTime(DateUtils.getNowDate());
        int i = baseMapper.updateById(lEmployee);
        getRedisList(0);
        return i;
    }

    /**
     * 批量删除员工
     *
     * @param empids 需要删除的员工主键
     * @return 结果
     */
    @Override
    public int deleteLEmployeeByEmpids(Integer[] empids) {
        int i = baseMapper.deleteLEmployeeByEmpids(empids);
        getRedisList(0);
        return i;
    }

    /**
     * 删除员工信息
     *
     * @param empid 员工主键
     * @return 结果
     */
    @Override
    public int deleteLEmployeeByEmpid(Integer empid) {
        return baseMapper.deleteById(empid);
    }

    @Override
    public List<LEmployee> getRedisList(int type) {

        redisCache.deleteObject("driver");
        redisCache.deleteObject("salesman");
        redisCache.deleteObject("employee");

        LEmployee param = new LEmployee();
        List<LEmployee> driverRedis = new ArrayList();
        List<LEmployee> salesmanRedis = new ArrayList();
        List<LEmployee> employeeRedis = new ArrayList();
        List<LEmployee> employeeList = baseMapper.selectLEmployeeList(param);
        employeeList.forEach(employee -> {
            if (employee.getIsdriver() == 1) {
                driverRedis.add(employee);
            }
            if (employee.getIssale() == 1) {
                salesmanRedis.add(employee);
            }
            employeeRedis.add(employee);
        });
        redisCache.setCacheList("salesman", salesmanRedis);
        redisCache.setCacheList("driver", driverRedis);
        redisCache.setCacheList("employee", employeeRedis);
        switch (type) {
            case 1:
                return employeeRedis;
            case 2:
                return salesmanRedis;
            case 3:
                return driverRedis;
        }
        return null;
    }

    @Override
    public Integer getId(LEmployee lEmployee) {

        List<LEmployee> employeeList= this.baseMapper.selectList(new LambdaQueryWrapper<LEmployee>(lEmployee));
        if(employeeList.size()>0)return employeeList.get(0).getEmpid();
        return 0;
    }
}
